package com.yn.xgame.game;

import com.yn.xgame.CommonMqMessageHandler;
import com.yn.xgame.GlobalSetting;
import com.yn.xgame.MQClient;
import com.yn.xgame.ZkClient;
import com.yn.xgame.util.Utils;

/**
 * GameApp
 * Created by wangboo on 17/7/18.
 */
public class GameApp {

    static GameConfig config;
    static ZkClient zkClient;
    static GameServer gameServer;
    static MQClient mqClient;

    public static void main(String[] args) throws Exception {
        config = new GameConfig("game1.cfg");
        if (config.getTopicList().size() == 0) throw new RuntimeException("topic list is null");
        gameServer = new GameServer();
        CommonMqMessageHandler handler = new CommonMqMessageHandler(gameServer);
        mqClient = new MQClient(config.getKafkaProp(), handler, config.getMqGroup(), config.getTopicList());
        // 将自身状态注册到zk
        zkClient = new ZkClient();
        // game
        // game/benz/
        // game/benz/1 - {topic, }
        zkClient.connect(config.getZkAddress(), null);
        zkClient.checkPath("/game");
        config.getGameNameList().forEach(gameName -> {
            String gamePath = "/game/" + gameName;
            zkClient.checkPath(gamePath);
        });
        config.getTopicList().forEach(gameTopic -> {
            int id = Utils.getNo(gameTopic);
            String gameName = Utils.getPrefixWord(gameTopic);
            String path = "/game/" + gameName + "/" + id;
            // 子游戏状态，承载，等
            zkClient.createOrsetData_EPHEMERAL(path, gameTopic);
        });
        gameServer.startup();
        mqClient.start();
        gameServer.join();
    }

    public static MQClient mqClient() {
        return mqClient;
    }

    public static GameServer gameServer() {return gameServer;}

}
